\documentclass[10pt,a4paper]{article} 

\usepackage{ctex} % 中文支持
\usepackage[top=2.5cm, bottom=2.5cm, left=2.5cm, right=2.5cm]{geometry} % 页边距
\usepackage{amsmath, amssymb} % 数学公式与符号
\usepackage{graphicx}
\usepackage{pythonhighlight}
\usepackage{url} 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{titling}
\setlength{\droptitle}{-2cm} % 标题上移

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 使表格美观
\usepackage{array}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\setlength\extrarowheight{3pt}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%文档的题目、作者与日期
\author{五六七 }
\title{企业生产计划与线性规划 }

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\maketitle

\begin{abstract}
某企业生产三种产品，已知需要的原材料、加工时间和产品利润。如何安排生产计划？
\end{abstract}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\setcounter{tocdepth}{2}
%\renewcommand\contentsname{目录}
%
%\renewcommand {\baselinestretch} {1.3}\normalsize 
%\tableofcontents 
%\renewcommand {\baselinestretch} {1.0}\normalsize


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{问题描述}
某企业利用两种原材料 A 和 B 生产三种产品 P1, P2 和 P3。
已知每生产1公斤产品所消耗的原材料的数量和加工时间，以及每公斤产品带来的利润。
请问该企业应该如何制定每天的生产计划，使所获利润最大？
\begin{table}[ht]\centering
\caption{三种产品的原材料、加工时间和利润 }\vspace{0.2cm}
\begin{tabular}{|M{2cm}|M{2cm}|M{2cm}|M{2cm}|M{2cm}|} \hline 
资源 & 产品P1 & 产品P2 & 产品P3 & 可用数量  \\ \hline 
原材料A & 2 & 4 & 3 & 150  \\ \hline 
原材料B & 3 & 1 & 5 & 160  \\ \hline 
加工时间 & 7 & 3 & 5 & 200  \\ \hline 
产品利润 & 70 & 50 & 60 &   \\ \hline 
\end{tabular}
\end{table}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{建立模型}
设企业每天生产这三种产品的数量分别为 $x_1, x_2, x_3$. 
则目标函数为每天的利润
\begin{eqnarray}
z= 70x_1 + 50x_2 + 60x_3. 
\end{eqnarray}

每天的原材料和加工时间的约束条件可以写成下述不等式。
另外，因为产品数量不能为负值，所以还有额外的约束条件。
\begin{eqnarray}
\left\{\begin{array}{l}
2x_1+4x_2+3x_3 \le 150, \\ 
3x_1+x_2+5x_3 \le 160, \\ 
7x_1+3x_2+5x_3 \le 200, \\
x_1 \ge 0,\,  
x_2 \ge 0,\, 
x_3 \ge 0. 
\end{array}\right. 
\end{eqnarray}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{编程计算}
有几种不同的 Python 程序包实现线性规划的计算。我们这里使用 scipy.optimize 模块。
也可以使用 cvxopt 或 cvxpy 程序包。 scipy.optimize 模块的 linprog 函数求解的线性规划问题的格式为
\begin{eqnarray}
\text{minimize} && z=c^Tx, \\ 
\text{subject to} &&  
\left\{\begin{array}{l}
Aub\cdot x \le b, \\ 
Aeq \cdot x= beq, \\ 
Lb\le x\le Ub.  
\end{array}\right. 
\end{eqnarray}

在本题中，目标函数 $z=c^Tx$ 具体为 
\begin{eqnarray}
 z=c^Tx=
 \begin{pmatrix} -70 & -50 & -60 \end{pmatrix} \cdot 
 \begin{pmatrix} x_1 \\ x_2 \\ x_3 \end{pmatrix}. 
\end{eqnarray}
约束条件 $Aub\cdot x \le b$ 具体为 
\begin{eqnarray}
\begin{pmatrix}  2&4&3 \\ 3&1&5 \\ 7&3&5  \end{pmatrix} \cdot 
\begin{pmatrix} x_1 \\ x_2 \\ x_3 \end{pmatrix} \le 
\begin{pmatrix} 150 \\ 160 \\ 200 \end{pmatrix}.  
\end{eqnarray}

linprog 函数的使用语法为
\begin{python}
linprog(
    c,
    A_ub=None,
    b_ub=None,
    A_eq=None,
    b_eq=None,
    bounds=None,
    method='interior-point',
    callback=None,
    options=None,
    x0=None,
)
\end{python}

首先载入scipy程序包的优化模块。
\begin{python}
import scipy.optimize as sco
sco.linprog?
\end{python}

设置目标函数与约束条件。注意到 linprog 函数的语法是求最小值，所以我们把目标函数的系数都改成相反数。
这样就把原问题求最大值，转化成求最小值。
\begin{python}
c=[-70, -50, -60]
A=[[2,4,3],[3,1,5],[7,3,5]]
b=[[150],[160],[200]]
\end{python}

调用 linprog 函数。因为决策变量的默认取值范围是非负实数，所以无需指定这个约束条件。
\begin{python}
res=sco.linprog(c,A,b)
print('Minimal value:',res.fun)
print('Minimal place:',res.x)
\end{python}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{回答问题}

%上述程序的输出结果如下。
%\begin{python}
%Minimal value: -2590.909027312059
%Minimal place: [ 4.95 18.59 21.92]
%\end{python}
%因此
企业每天生产产品P1、P2和P3 分别为 4.95 公斤、18.59公斤和21.92公斤的时候，可得最大利润 2590.91元。



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\section{参考文献 }
\begin{thebibliography}{99}
\bibitem{sishoukui-2} 司守奎,孙玺菁. \emph{Python数学建模算法与应用}, 国防工业出版社. 2022年1月第1版. 
\bibitem{scipy-linprog}
\url{https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.linprog.html}
\bibitem{cvxopt} \url{https://cvxopt.org/index.html}.
\bibitem{cvxpy} \url{https://www.cvxpy.org}. 
\end{thebibliography}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

